package es.deusto.ingenieria.aike.puzzle;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import es.deusto.ingenieria.aike.puzzle.Car.Direction;
import es.deusto.ingenieria.aike.xml.InformationXMLReader;


public class EnvironmentXMLReader extends InformationXMLReader
{
        private Board board;
        private Car car;
        private Flag flag;
        private Integer cont;

        public EnvironmentXMLReader(String xmlFile) {           
                super(xmlFile);
                cont = 0;
        }
        
        public Object getInformation() {
                return new Environment(this.board, this.car,this.flag);
        }

        public Car getCar() {
                return car;
        }

        public Flag getFlag() {
                return flag;
        }

        public Integer getCont() {
                return cont;
        }

        public void startElement(String uri, String localName, String qName,
                        Attributes attributes) throws SAXException {
        
                try {           
                        
                        if (qName.equals("aike:maze"))
                        {
                                Integer[] tam = new Integer[2];
                                tam[0]= Integer.parseInt(attributes.getValue("rows"))+ 2;
                                tam[1]= Integer.parseInt(attributes.getValue("columns"))+ 2;
                                this.board = new Board(tam);
                        } 
                        else  if (qName.equals("aike:car"))
                        {
                                
                                Integer[]pos = new Integer[2];
                                pos[0]= Integer.parseInt(attributes.getValue("row"));
                                pos[1]= Integer.parseInt(attributes.getValue("column"));
                                int y= Integer.parseInt(attributes.getValue("row"));
                                int x= Integer.parseInt(attributes.getValue("column"));
                                Direction dir;
                                if (pos[0] == 0)
                                {
                                	dir = Direction.DOWN;
                                }
                                else if (pos[0] == this.board.getTam()[0]-1)
                                {
                                	dir = Direction.UP;
                                }
                                else if (pos[1] == 0)
                                {
                                	dir = Direction.RIGHT;
                                }
                                else
                                {
                                	dir = Direction.LEFT;
                                }                        		
                                this.car = new Car(dir, y, x);  
                                this.board.setTile(pos, 'c');
                        } 
                        else if (qName.equals("aike:flag")) 
                        {
                                Integer[]pos = new Integer[2];
                                pos[0] = Integer.parseInt(attributes.getValue("row"));
                                pos[1] = Integer.parseInt(attributes.getValue("column"));
                                boolean[]flagPos = new boolean[4];
                                flagPos[0] = (attributes.getValue("left-wall").equalsIgnoreCase("yes"));
                                flagPos[1] = (attributes.getValue("top-wall").equalsIgnoreCase("yes"));
                                flagPos[2] = (attributes.getValue("right-wall").equalsIgnoreCase("yes"));
                                flagPos[3] = (attributes.getValue("bottom-wall").equalsIgnoreCase("yes"));                              
                                this.flag = new Flag(pos, flagPos);
                                this.board.setTile(pos, 'f');
                        }
                        else  if (qName.equals("aike:crosses"))
                        {
                        
                        }
                        else if (qName.equals("aike:cross"))
                        {
                                Integer[]pos = new Integer[2];
                                pos[0] = Integer.parseInt(attributes.getValue("row"));
                                pos[1] = Integer.parseInt(attributes.getValue("column"));
                                board.setTile(pos, 'x');
                        }
                        
                } catch (Exception ex) {
                        System.out.println(this.getClass().getName() + ".startElement(): " + ex);
                }
        }
        
        public void imprimir()
    	{
    		this.board.toString();
    	}
}